﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Windows.Forms;
using Cybertron_Tracker.Cybertron;

namespace Cybertron_Tracker
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            icoLoading.Visible = true;
            Hashtable loginOutput = RedMine.login(txtUsername.Text, txtPassword.Text);

            String error = (String)loginOutput["error"];
            if (error != null)
            {
                MessageBox.Show(error, "Login error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            verifyAuth();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            verifyAuth();
        }

        private void verifyAuth()
        {
            icoLoading.Visible = true;
            if (RedMine.isAuthenticated())
            {
                showMainForm();
            }
            icoLoading.Visible = false;
        }

        private void showMainForm()
        {
            if (chkRemember.Checked)
            {
                
            }

            frmMain MainApp = new frmMain();
            MainApp.Show();
            this.Hide();
        }

        private void txtUsername_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                btnLogin_Click(sender, e);
            }
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            txtUsername_KeyUp(sender, e);
        }

    }
}
